package cn.kduck.module.message.service;


import cn.kduck.module.message.utils.MimeTypeUtils;

import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.activation.URLDataSource;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MailMessage {

    private String mailCode;
    private String fromUserName;
    private String[] to;
    private String[] cc;
    private String[] bcc;
    private String subject;
    private String content;

    private List<DataSource> attachmentList = new ArrayList<>(5);
    private Map<String,DataSource> inlineMap = new HashMap<>();

    public String getMailCode() {
        return mailCode;
    }

    public void setMailCode(String mailCode) {
        this.mailCode = mailCode;
    }

    public String[] getTo() {
        return to;
    }

    public void setTo(String[] to) {
        this.to = to;
    }

    public void setTo(String to) {
        this.to = new String[]{to};
    }

    public String[] getCc() {
        return cc;
    }

    public void setCc(String[] cc) {
        this.cc = cc;
    }

    public String[] getBcc() {
        return bcc;
    }

    public void setBcc(String[] bcc) {
        this.bcc = bcc;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getFromUserName() {
        return fromUserName;
    }

    public void setFromUserName(String fromUserName) {
        this.fromUserName = fromUserName;
    }

    public void addAttachment(String fileName, URL attachment) {
        attachmentList.add(new URLDataSource(attachment){
            @Override
            public String getName() {
                return fileName;
            }
        });
    }

    public void addAttachment(File attachment) {
        attachmentList.add(new FileDataSource(attachment));
    }

    public void addAttachment(String fileName,InputStream attachment) {

        attachmentList.add(new DataSource(){
            @Override
            public InputStream getInputStream() throws IOException {
                return attachment;
            }

            @Override
            public OutputStream getOutputStream() throws IOException {
                throw new UnsupportedOperationException("Read-only javax.activation.DataSource");
            }

            @Override
            public String getContentType() {
                int dotPos = fileName.lastIndexOf(".");
                if (dotPos < 0) {
                    return MimeTypeUtils.DEFAULT_TYPE;
                } else {
                    String fileExt = fileName.substring(dotPos + 1);
                    if (fileExt.length() == 0) {
                        return MimeTypeUtils.DEFAULT_TYPE;
                    } else {
                        return MimeTypeUtils.getMimeType(fileExt);
                    }
                }
            }

            @Override
            public String getName() {
                return fileName;
            }
        });
    }

    public void addInline(String cid,URL attachment) {
        inlineMap.put(cid,new URLDataSource(attachment));
    }

    public void addInline(String cid,File attachment) {
        inlineMap.put(cid,new FileDataSource(attachment));
    }

    public void addInline(String cid,InputStream inlineStream) {
        inlineMap.put(cid,new DataSource(){
            @Override
            public InputStream getInputStream() throws IOException {
                return inlineStream;
            }

            @Override
            public OutputStream getOutputStream() throws IOException {
                throw new UnsupportedOperationException("Read-only javax.activation.DataSource");
            }

            @Override
            public String getContentType() {
                return MimeTypeUtils.DEFAULT_TYPE;
            }

            @Override
            public String getName() {
                return cid;
            }
        });
    }

    public List<DataSource> getAttachmentList() {
        return attachmentList;
    }

    public Map<String,DataSource> getInlineMap() {
        return inlineMap;
    }
}
